package com.lyh.hkcloud.service;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import androidx.annotation.RequiresApi;
import androidx.core.content.FileProvider;
import com.lyh.hkcloud.constants.*;
import com.lyh.hkcloud.module.common.UpdateManager;
import com.lyh.hkcloud.util.base.*;
import com.lyh.hkcloud.util.http.Request;
import com.lyh.hkcloud.util.http.listener.json.JsonFailuredListener;
import com.lyh.hkcloud.util.http.listener.json.JsonSuccessListener;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class AppService {
    private Activity activity;

    public AppService(Activity activity) {
        this.activity = activity;
    }

    public void getApps(final JsonSuccessListener successListener) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("softwareCode", "assets_007");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Request.post(activity, HttpUrlComstants.DOMAIN_NAME_URL+HttpUrlComstants.NEW_VERSION, jsonObject, new JsonSuccessListener() {
            @RequiresApi(api = Build.VERSION_CODES.P)
            @Override
            public void onRespone(String msg, JSONObject response) {
                if (!"0000".equals(response.optString("code"))) {
                    ToastUtils.makeText(activity,"获取更新包失败"+ response.optString("message"), ToastUtils.LENGTH_SHORT).show();
                } else {
                    JSONObject data = response.optJSONObject("data");
                    if(null==data){
                        Log.e("error","获取更新包失败");
                    }
                    UpdateManager updateManager = new UpdateManager(activity,data);
                    updateManager.checkUpdateInfo();
                    successListener.onRespone(msg, response);
                }
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error","获取更新包失败:"+response.optString("message"));
                ToastUtils.makeText(activity, msg, ToastUtils.LENGTH_SHORT).show();
            }
        });
    }

    public void getMenus(int appId, final JsonSuccessListener successListener, JsonFailuredListener failuredListener) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("appID", appId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Request.post(activity, UrlConstants.BASE_URL + "/eip-mapp-umc-server/workbench/sysFunAppDetailTreeByAppID", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                if (!"0000".equals(response.optString("code"))) {
                    ToastUtils.makeText(activity, response.optString("msg"), ToastUtils.LENGTH_SHORT).show();
                } else {
                    successListener.onRespone(msg, response);
                }
            }
        }, failuredListener);
    }

    public void checkUpdate(final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("appSoftwareCode", AppConstants.SOFTWARE);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Request.post(activity, "UrlConstants.BASE_URL" + "/eip-mapp-umc-server/appSoftware/findAppSoftwareNewVersion", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                if (!"0000".equals(response.optString("code"))) {
                    ToastUtils.makeText(activity, "检测更新失败，请联系管理员。", ToastUtils.LENGTH_SHORT).show();
                    failuredListener.onRespone(response.optString("msg"),response);
                } else {
                    successListener.onRespone(msg, response.optJSONObject("data"));
                }
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                failuredListener.onRespone("服务连接失败，请检查网络是否正常！", response);
            }
        });
    }

    public void openAPK(String fileSavePath) {
        File apkfile = new File(fileSavePath);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (!apkfile.exists()) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            try {
                Uri contentUri = FileProvider.getUriForFile(activity, "com.lyh.hkcloud.FileProvider", apkfile);
                intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        activity.startActivity(intent);
        activity.finish();
    }

}
